/*
 *             Copyright (C) 2024 Steve Jrong
 *
 * 	   GitHub Homepage: https://www.github.com/SteveJrong
 *      Gitee Homepage: https://gitee.com/stevejrong1024
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.stevejrong.waiter.rp.config;

import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.MQProducer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import com.stevejrong.waiter.mq.message.component.DomainEventPublisher;

import jakarta.inject.Named;
import lombok.Data;

/**
 * 配置类 - 宿主应用认证授权功能相关的RocketMQ生产者
 *
 * @author Steve Jrong
 * @since 1.0
 */
@Data
@Configuration
public class AuthAppMQProducerConfig {

    @Value("${waiter.auth.rocketmq.name-server}")
    private String nameServerAddr;

    @Value("${waiter.auth.rocketmq.producer.group}")
    private String producerGroup;

    /**
     * 适用于认证授权中心的消息生产者
     *
     * @return 适用于认证授权中心的消息生产者Bean
     * @throws MQClientException 消息客户端创建异常
     */
    @Bean
    public MQProducer waiterAuthAppProducer() throws MQClientException {
        DefaultMQProducer producer = new DefaultMQProducer(producerGroup); // 创建消息生产者
        producer.setNamesrvAddr(nameServerAddr); // NameServer的地址
        producer.start(); // 启动生产者组
        return producer;
    }

    /**
     * 适用于认证授权中心的领域事件发布器
     *
     * @param waiterAuthAppProducer 适用于认证授权中心的消息生产者Bean
     * @return 适用于认证授权中心的领域事件发布器Bean
     */
    @Bean
    @DependsOn("waiterAuthAppProducer")
    public DomainEventPublisher authAppDomainEventPublisher(@Named("waiterAuthAppProducer") MQProducer waiterAuthAppProducer) {
        return new DomainEventPublisher(waiterAuthAppProducer);
    }
}